using System;
using System.Collections.Generic;
using System.Collections;
using System.Windows.Forms;

namespace cs2335.CCT
{
    public class CCTForm : Form, ICCTForm 
	{
		private ArrayList componentList;
		private IComponent focused;


        #region ICCTForm Members

        public void Add(IComponent comp)
        {
            componentList.Add(comp);
            comp.Owner = this;
        }

        public void Remove(IComponent killMeNow)
        {
            killMeNow.Hide();
            componentList.Remove(killMeNow);
            killMeNow.Owner = null;
        }

        #endregion
        
	
		public CCTForm() 
		{
			componentList = new ArrayList();
			this.MouseClick += new MouseEventHandler(OnMouseClick);
            this.KeyPress += new KeyPressEventHandler(OnKeyPress);
            this.KeyDown += new KeyEventHandler(OnKeyDown);
		}

        override protected void OnPaint(PaintEventArgs e)
		{
            base.OnPaint(e);
            foreach(IComponent c in componentList)
                c.OnPaint(e);
		}
	
		public void OnKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
            if (focused != null) focused.OnKeyPress(e);
		}

        public void OnKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            int tempIndex;
            if (e.KeyCode == Keys.Tab)
            {
                if (e.Modifiers == Keys.Shift)
                {
                    if (focused == null && componentList != null)
                    {
                        focused = (IComponent)componentList[0];
                        focused.OnGotFocus(e);
                    }
                    else if (focused != null && componentList != null)
                    {
                        focused.OnLostFocus(e);
                        tempIndex = componentList.IndexOf(focused);
                        if (0 == tempIndex)
                        {
                            tempIndex = componentList.Count - 1;
                        }
                        else
                        {
                            tempIndex--;
                        }
                        focused = (IComponent)componentList[tempIndex];
                        focused.OnGotFocus(e);
                    }

                }
                else
                {
                    if (focused == null && componentList != null)
                    {
                        focused = (IComponent)componentList[0];
                        focused.OnGotFocus(e);
                    }
                    if (focused != null && componentList != null)
                    {
                        focused.OnLostFocus(e);
                        tempIndex = componentList.IndexOf(focused);
                        if ((componentList.Count - 1) == tempIndex)
                        {
                            tempIndex = 0;
                        }
                        else
                        {
                            tempIndex++;
                        }
                        focused = (IComponent)componentList[tempIndex];
                        focused.OnGotFocus(e);
                    }
                }
            }
            if (focused != null) focused.OnKeyDown(e);
        }

        public void OnMouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (focused != null) focused.OnLostFocus(e);
            foreach (IComponent c in componentList)
            {
                if (c.Bounds.Contains(e.X, e.Y))
                {
                    c.OnMouseClick(e);
                    c.OnGotFocus(e);
                    focused = c;
                    return;
                }
            }
            focused = null;
        }

       
    }
}
